///////////////Simulations file for "How Much Should We Trust the Dictator's GDP Growth Estimates?" by Luis R. Martinez. last edited: 2021/12/15

/*

///////////////////////NOTES//////////////////////////////////

//Description:

This do-file carries out Montecarlo simulations for the model in Appendix C.5. and produces Figure C1

// Instructions:

*1. Fix global "dir" immediately below  to the destination folder
*2. Destination folder must include following folders:
-"Data" w/ following subfolders:
	-"temp" which includes intermediate files with regression estimates produced during execution of do-file
		-"simulations" which holds copies of each simulation result
-"Output" which includes the following subfolders:
	-"Figures", which includes Figure C1 produced during execution of do-file in PDF format
	
//	Computational Environment:

Operating System: Windows 10
Stata version: Stata/MP 17.0 for Windows (64 bit)
Estimated running time: 20 minutes
	
/////////////////////////////////////////////////////////////

*/

clear all
set more off
global dir "XXX"	

global figures "${dir}\Output\Figures"
global simulations "${dir}\Data\temp\simulations"

set seed 20051702	//seed used for replication
global simuls=10000	//number of simulations

/*sample size*/
global N=184 /*Number of Countries*/
global T=22 /*Number of Years */

/*parameters defined as in paper, representative numerical values selected*/
global alpha=-1 /*close to ANRR 2019*/
global beta=1
global gamma_d=1
global gamma_a=0.8
global s=0.60 /*paper's estimate for full change from FiW = 0 to FiW = 6*/
global d=1 /*range of e and u*/

set obs ${N}
gen country_id=_n
gen fiw=runiform(-0.5,0.5) /*autocracy ranges continuously from 0 to 6, which I adjust for mean zero*/
sort country_id
tempfile country_stub
save `country_stub'

clear
set obs ${T}
gen year=_n+1991
cross using `country_stub'
sort country_id year

forvalues x=1/$simuls{
	gen e=runiform(-${d},${d})
	gen u=runiform(-${d},${d})
	gen yd=runiform(-2.5,2.5) /*democratic growth rates uniformly range from 0 to 5, which I adjust for mean zero*/
	gen y=yd+${alpha}*fiw
	gen gdp=${beta}*y+e
	gen gdp_hat=(1+${s}*fiw)*gdp
	gen ntl=(${gamma_d}*yd)+(${gamma_a}*${alpha}*fiw)+u
	gen ntl_fiw=ntl*fiw
	reghdfe gdp_hat ntl ntl_fiw, absorb(country_id year)
	nlcom (3.5/6)*_b[ntl_fiw]/_b[ntl],post /*3.5 is the IQR of FiW index, which ranges from 0 to 6*/
	parmest,saving(${simulations}\simul`x',replace) idnum(`x')
	drop e-ntl_fiw
}

clear
forvalues x=1/$simuls{
    append using ${simulations}\simul`x'
}
histogram estimate, xtitle({&sigma} estimate) xline(0.35,lcolor(red)) ylabel(, angle(horizontal)) scheme(sj) graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) plotregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white)) 
graph export "${figures}\figure_C1.pdf", as(pdf) replace